home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / c / stdlib / div.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-22  |  1.5 KB  |  60 lines

  1. /* 
  2.  * div.c --
  3.  *
  4.  *    Contains the source code for the "div" library procedure.
  5.  *
  6.  * Copyright 1988 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /sprite/src/lib/c/stdlib/RCS/div.c,v 1.1 88/05/21 12:14:42 ouster Exp $ SPRITE (Berkeley)";
  18. #endif not lint
  19.  
  20. #include "stdlib.h"
  21.  
  22. /*
  23.  *----------------------------------------------------------------------
  24.  *
  25.  * div --
  26.  *
  27.  *    Compute the quotient and remainder of the division of numer
  28.  *    by denom.
  29.  *
  30.  * Results:
  31.  *    The return value is j, unless j is negative, in which case
  32.  *    the return value is -j.
  33.  *
  34.  * Side effects:
  35.  *    None.
  36.  *
  37.  *----------------------------------------------------------------------
  38.  */
  39.  
  40. div_t
  41. div(numer, denom)
  42.     int numer;            /* Number to divide into. */
  43.     int denom;            /* Number that's divided into it. */
  44. {
  45.     div_t result;
  46.  
  47.     result.quot = numer/denom;
  48.     result.rem = numer%denom;
  49.     if ((result.rem ^ numer) < 0) {
  50.     if (result.rem < 0) {
  51.         result.rem += denom;
  52.         result.quot -= 1;
  53.     } else {
  54.         result.rem -= denom;
  55.         result.quot += 1;
  56.     }
  57.    }
  58.    return result;
  59. }
  60.